package cn.school.test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Scanner;

import org.apache.ibatis.annotations.Case;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import cn.school.dao.SubjectDao;
import cn.shcool.entity.Subject;

public class MyBatisTest {

	public static void main(String[] args) throws Exception {

		Scanner in = new Scanner(System.in);
		SqlSession session = null;
		String resource = "mybatis-config.xml";
		InputStream inputStream;

		try {
			inputStream = Resources.getResourceAsStream(resource);
			SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder()
					.build(inputStream);
			session = sqlSessionFactory.openSession(true);

			SubjectDao is = session.getMapper(SubjectDao.class);

			System.out.println("请输入一下操作");
			System.out.println("1,查询  2，增加 数据  3.删除数据  4.修改数据  5，数据分页 6，统计");
			int a = in.nextInt();
			Subject s = new Subject();
			switch (a) {
			case 1: {
				// 查询
			}
				List<Subject> list = is.findALL();
				for (Subject s1 : list) {
					System.out.println(s1);
				}
			case 2: {
				// 增加
				System.out.println("请输入 ，学科名称，课时，年级号");
				String suname = in.next();
				int hour = in.nextInt();
				int gradeid = in.nextInt();
				s.setSubjectname(suname);
				s.setClassHour(hour);
				s.setGradeid(gradeid);
				is.add(s);
			}
			case 3: {
				// 删除
				System.out.println("请输入要删除的学号");
				int id = in.nextInt();
				s.setId(id);
				is.del(s);
			}
			case 4: {
				// 修改
				System.out.println("请输入要修改的学号，课程名 ， 课时，年级");
				int id = in.nextInt();
				String suname = in.next();
				int hour = in.nextInt();
				int gradeid = in.nextInt();
				s.setSubjectname(suname);
				s.setClassHour(hour);
				s.setGradeid(gradeid);
				int i = is.xg(s);
				System.out.println(i);
			}
			case 5: {
				// 分页
				System.out.println("请输入页数和 页个数");
				int num = in.nextInt();
				int size = in.nextInt();
				List<Subject> list1 = is.paging(num, size);
				for (Subject o : list1) {
					System.out.println(o);
				}
			}
			case 6: {
				// 统计
				int i = is.total();
				System.out.println(i);
			}
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {

		}
	}
}
